﻿#include "view.h"
#include<QDebug>

view* view::v=nullptr;

view::view(QWidget*parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
    initControl();
}

view::~view()
{
    qDebug()<<"view delete";
    delete g;
    for(QList<fish*>::iterator itor=fishList.begin();itor!=fishList.end();++itor)
    {
        delete *itor;
    }
}

void view::initControl()
{
    scene=new QGraphicsScene(this);
    scene->setSceneRect(0,0,1000,700);
    setFixedSize(1002,702);
    g=new gun("./gun.jpeg",scene);
    for(int i=0;i<5;i++)
    {
        fishList.append(new fish("./fish.jpeg",scene));
    }
    timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),scene,SLOT(advance()));
    timer->start(100);
    setScene(scene);
    setMouseTracking(true);//配合鼠标移动事件
}

view *view::getInstance()
{
    if(v==nullptr)
    {
        v=new view();
    }
    return v;
}

void view::mouseMoveEvent(QMouseEvent *event)
{
    QPoint p=event->pos();
    QLine line(width()/2,height(),p.x(),p.y());
    QLineF lineF(line);
    g->setRotation(90-lineF.angle());
}

void view::mousePressEvent(QMouseEvent *event)
{
    QPoint p=event->pos();
    QLineF lineF(width()/2,height(),p.x(),p.y());
    bullet *b=new bullet("./bullet.jpeg",scene,90-lineF.angle());
}
